import datetime
import os
import re

r_name = re.compile(r'\+name = (.*)')
r_name_e = re.compile(r'\+name_e = (.*)')
r_language = re.compile(r'\+language = (.*)')
r_picture_src = re.compile(r'\+picture_src = (.*)')
r_composer = re.compile(r'\+composer = (.*)')
r_birthday = re.compile(r'\+birthday = (\d*)年(\d*)月(\d*)日')
r_intro = re.compile(r'\+intro = ([\s\S]*)')
r_vocal_part = re.compile(r'\+vocal_part = (.*)')
r_nation = re.compile(r'\+nation = (.*)')


def handle_opera():
    # class Opera(db.Model):
    #     name = db.Column(db.String(50), primary_key=True)
    #     name_e = db.Column(db.String(50))
    #     language = db.Column(db.String(20))
    #     intro = db.Column(db.String(500))
    #     picture_src = db.Column(db.String(50))
    #     composer = db.Column(db.String(50), db.ForeignKey('composer.name'))
    #     song = db.relationship('Song', backref='song', lazy=True)
    #     character = db.relationship('Character', backref='character', lazy=True)
    # 扫描目录
    opera_dir = './opera/'
    file_list = os.listdir(opera_dir)
    out = open('./opera-script.txt', mode='w', encoding='utf-8')
    for f in file_list:
        fin = open(opera_dir + f, mode='r', encoding='utf-8')
        fread = fin.read()
        name = r_name.findall(fread)[0]
        name_e = r_name_e.findall(fread)[0]
        language = r_language.findall(fread)[0]
        picture_src = r_picture_src.findall(fread)[0]
        composer_name = r_composer.findall(fread)[0]
        intro = r_intro.findall(fread)[0]

        res_str = "db.session.add(Opera(name='"
        res_str += name + "', name_e='" + name_e + "', language='"
        res_str += language + "', picture_src='" + picture_src + "', composer_name='"
        res_str += composer_name + "', intro='''" + intro + "'''))\n"
        print(res_str)
        out.write(res_str)
    out.write("db.session.commit()\n")
    out.close()


def handle_singer():
    # class Singer(db.Model):
    #     name = db.Column(db.String(50), primary_key=True)
    #     name_e = db.Column(db.String(50))
    #     birthday = db.Column(db.Date)
    #     vocal_part = db.Column(db.String(20))  # 声部
    #     picture_src = db.Column(db.String(50))
    #     intro = db.Column(db.String(500))
    # 扫描目录
    obj = 'singer'
    cla = 'Singer'
    my_dir = './' + obj + '/'
    file_list = os.listdir(my_dir)
    out = open('./' + obj + '-script.txt', mode='w', encoding='utf-8')
    for f in file_list:
        fin = open(my_dir + f, mode='r', encoding='utf-8')
        fread = fin.read()
        name = r_name.findall(fread)[0]
        name_e = r_name_e.findall(fread)[0]
        birthday = r_birthday.findall(fread)[0]
        vocal_part = r_vocal_part.findall(fread)[0]
        picture_src = r_picture_src.findall(fread)[0]
        intro = r_intro.findall(fread)[0]

        res_str = "birthday = datetime.date(%s, %s, %s)\n" % (birthday[0], birthday[1], birthday[2])
        res_str += "db.session.add(" + cla + "(name='"
        res_str += name + "', name_e='" + name_e + "', birthday=birthday"
        res_str += ", vocal_part='" + vocal_part + "', picture_src='"
        res_str += picture_src + "', intro='''" + intro + "'''))\n"
        print(res_str)
        out.write(res_str)
    out.write("db.session.commit()\n")
    out.close()


def handle_composer():
    # class Composer(db.Model):
    #     name = db.Column(db.String(50), primary_key=True)
    #     name_e = db.Column(db.String(50))
    #     birthday = db.Column(db.Date)
    #     nation = db.Column(db.String(50))
    #     intro = db.Column(db.String(500))
    #     operas = db.relationship('Opera', backref='composer', lazy=True)
    #     picture_src = db.Column(db.String(50))
    # 扫描目录
    obj = 'composer'
    cla = 'Composer'
    my_dir = './' + obj + '/'
    file_list = os.listdir(my_dir)
    out = open('./' + obj + '-script.txt', mode='w', encoding='utf-8')
    for f in file_list:
        fin = open(my_dir + f, mode='r', encoding='utf-8')
        fread = fin.read()
        name = r_name.findall(fread)[0]
        name_e = r_name_e.findall(fread)[0]
        birthday = r_birthday.findall(fread)[0]
        nation = r_nation.findall(fread)[0]
        picture_src = r_picture_src.findall(fread)[0]
        intro = r_intro.findall(fread)[0]

        res_str = "birthday = datetime.date(%s, %s, %s)\n" % (birthday[0], birthday[1], birthday[2])
        res_str += "db.session.add(" + cla + "(name='"
        res_str += name + "', name_e='" + name_e + "', birthday=birthday"
        res_str += ", nation='" + nation + "', picture_src='"
        res_str += picture_src + "', intro='''" + intro + "'''))\n"
        print(res_str)
        out.write(res_str)
    out.write("db.session.commit()\n")
    out.close()


if __name__ == "__main__":
    # handle_composer()
    # handle_opera()
    # handle_singer()
    pass
